<html>
<head><meta charset="utf-8"><title>Isn&#x27;t a pointer to a sized type always FFI safe? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html">Isn&#x27;t a pointer to a sized type always FFI safe?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219120762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219120762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219120762">(Dec 07 2020 at 18:47)</a>:</h4>
<p>Given </p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">SDL_Cursor</span><span class="p">([</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="mi">0</span><span class="p">]);</span><span class="w"></span>
</code></pre></div>
<p>Rust gives me a warning on an <code>extern "C"</code> declaration that uses pointers to this:</p>
<div class="codehilite"><pre><span></span><code>warning: `extern` block uses type `SDL_Cursor`, which is not FFI-safe
   --&gt; src\mouse.rs:214:33
    |
214 |   pub fn SDL_FreeCursor(cursor: *mut SDL_Cursor);
    |                                 ^^^^^^^^^^^^^^^ not FFI-safe
    |
    = help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
    = note: this struct has unspecified layout
</code></pre></div>
<p>Is this just the warning being overly defensive? It seems like a pointer to a sized type is always itself FFI safe, regardless of what repr the type is.</p>



<a name="219120974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219120974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219120974">(Dec 07 2020 at 18:48)</a>:</h4>
<p>C doesn't have zero sized types, and a pointer depends on the size of the pointee for pointer math</p>



<a name="219122316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219122316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219122316">(Dec 07 2020 at 18:58)</a>:</h4>
<p>Ah, and <code>c_void</code> is actually size 1 instead of size 0. I follow now.</p>



<a name="219125020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219125020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219125020">(Dec 07 2020 at 19:17)</a>:</h4>
<p>By the way, why are you using an empty array and not</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">SDL_Cursor</span><span class="p">;</span><span class="w"></span>
</code></pre></div>
<p>or</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">SDL_Cursor</span><span class="p">(());</span><span class="w"></span>
</code></pre></div>
<p>if you want the type's constructor to be private?</p>



<a name="219132250"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219132250" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219132250">(Dec 07 2020 at 20:14)</a>:</h4>
<p>It's still an overly defensive lint, provided the <code>C</code> side defines that type as an opaque one (in which case it will literally not be able to perform pointer arith)</p>



<a name="219132975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219132975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219132975">(Dec 07 2020 at 20:20)</a>:</h4>
<p>You could make it a <code>#[repr(transparent)]</code> wrapper around <code>c_void</code> I think.</p>



<a name="219153082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219153082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219153082">(Dec 07 2020 at 23:29)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> I started with the empty array because that's what <code>bindgen</code> had output (in a previous version of the lib that used bindgen)</p>



<a name="219153179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219153179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219153179">(Dec 07 2020 at 23:30)</a>:</h4>
<p>After the feedback from Steven F. I decided to go with this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[repr(transparent)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">struct</span> <span class="nc">SDL_Cursor</span><span class="p">(</span><span class="n">c_void</span><span class="p">);</span><span class="w"></span>
</code></pre></div>
<p>Which I think is much more obviously "you will only use this as a pointer".</p>



<a name="219153303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219153303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219153303">(Dec 07 2020 at 23:32)</a>:</h4>
<p>As to what Daniel HM said: the C definition of the type is</p>
<div class="codehilite" data-code-language="C"><pre><span></span><code><span class="k">typedef</span> <span class="k">struct</span> <span class="nc">SDL_Cursor</span> <span class="n">SDL_Cursor</span><span class="p">;</span>
</code></pre></div>



<a name="219153327"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219153327" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219153327">(Dec 07 2020 at 23:32)</a>:</h4>
<p>Rather, that's the definition of the type as seen in the public API header file</p>



<a name="219193441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Isn%27t%20a%20pointer%20to%20a%20sized%20type%20always%20FFI%20safe%3F/near/219193441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Isn&#x27;t.20a.20pointer.20to.20a.20sized.20type.20always.20FFI.20safe.3F.html#219193441">(Dec 08 2020 at 10:55)</a>:</h4>
<p>That definition is equivalent to <code>extern { type SDL_Cursor; }</code> in rust, but that is currently unstable.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>